/*
 * =====================================================================================
 *
 *       Filename:  perror.c
 *
 *    Description:  demo of perror
 *
 *        Version:  1.0
 *        Created:  04/13/2012 03::08:34 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Zhiyi Wu (Z.W), zhiyiw@uoregon.edu
 *        Company:  University of Oregon
 *
 * =====================================================================================
 */

#include	"stdio.h"
#include	"unistd.h"
#include	"string.h"
#include	"stdlib.h"
	void
main (  )
{
	int byteCount;	//actual number of bytes read from read()
	char *arg[] = {"", NULL};//arg for execve
	char *env[] = {"",NULL};//empty arrays for execve
	char cmd[1024];  //command you typed
	char feedback[1024]="This is your command result:\n";
	char welcome[1024]="Please input your command(filename with path):";//promopt the input
	write(1, welcome, sizeof(welcome));//write welcome to screen
	if((byteCount=read(0,cmd,1024))<0) //get the size of the input
	{perror("Read failed"); exit(1);} //check for errors
	//	byteCount=read(0,cmd,1024);

	cmd[byteCount-1]='\0';
	write(1,feedback,sizeof(feedback));
	if(-1==execve(cmd,arg,env) )	//exec the command
	{perror("execve failed"); exit(1);} //error check

	//execve(cmd,arg,env);
	return ;
}		/* -----  end of function main  ----- */
